package word427.chapter21;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public class HashMapzy {
    public static void main(String[] args) {
        Map<Integer,Student> stuMap = new HashMap<>(); //先创建一个空的HaseMap对象
        Student stu1 = new Student("魏十一",18,"男");
        Student stu2 = new Student("老丘",22,"男");    //实例化三个学生对象
        Student stu3 = new Student("十一姐",20,"女");

        stuMap.put(2021044,stu1);  //将实例化后的学生对象放进去
        stuMap.put(2021045,stu2);  //将实例化后的学生对象放进去
        stuMap.put(2021046,stu3);  //将实例化后的学生对象放进去

        //将stuMap里的对象全部打印出来
        for(int i : stuMap.keySet()){
            System.out.println("学号："+i+" "+stuMap.get(i).toString());
        }

        System.out.println("------------------TreeMap分割线----------------------------");
        //将stuMap里的对象全部打印出来
        Map<Integer,Student> treeMap = new TreeMap<>(stuMap);
        for(int j : treeMap.keySet()){
            System.out.println("学号："+j+" "+stuMap.get(j).toString());
        }


        System.out.println("------------------linkedHashMap分割线----------------------------");
        Map<Integer, Student> linkedHashMap =
                new LinkedHashMap<>(16, 0.75f, true);
        linkedHashMap.put(2021044,stu1);  //将实例化后的学生对象放进去
        linkedHashMap.put(2021045,stu2);  //将实例化后的学生对象放进去
        linkedHashMap.put(2021046,stu3);  //将实例化后的学生对象放进去

        System.out.println("学号为2023045的学生为："+linkedHashMap.get(2021045));

        System.out.println("linkedHashMap里的元素有"+linkedHashMap);

        //将键和值对应打印出来
        System.out.print("\n将键和值对应打印出来");
        treeMap.forEach(
                (name, age) -> System.out.print(name + ": " + age + " "));




    }
}
